﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ResidencyPlacement;
using System.Windows.Controls.Primitives;
using System.ComponentModel;

namespace ResidencyPlacementWPF
{
    /// <summary>
    /// Interaction logic for ViewHospitalApplications.xaml
    /// </summary>
    public partial class ViewHospitalApplications : Window
    {
        private Hospital hospital;

        public ViewHospitalApplications()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CollectionViewSource hospitalAppliedViewSource = (CollectionViewSource)(this.FindResource("hospitalAppliedViewSource"));
            hospitalAppliedViewSource.Source = hospital.Applied;

            CollectionViewSource hospitalInterviewedViewSource = (CollectionViewSource)(this.FindResource("hospitalInterviewedViewSource"));
            hospitalInterviewedViewSource.Source = hospital.Interviewed;

            CollectionViewSource hospitalSelectedViewSource = (CollectionViewSource)(this.FindResource("hospitalSelectedViewSource"));
            hospitalSelectedViewSource.Source = hospital.Selected;

            CollectionViewSource hospitalOfferAcceptedViewSource = (CollectionViewSource)(this.FindResource("hospitalOfferAcceptedViewSource"));
            hospitalOfferAcceptedViewSource.Source = hospital.OfferAccepted;

            CollectionViewSource hospitalDeclinedViewSource = (CollectionViewSource)(this.FindResource("hospitalDeclinedViewSource"));
            hospitalDeclinedViewSource.Source = hospital.Declined;

            if (hospital.OfferAccepted.Count == hospital.NumberOfPositions)
                offerButton.IsEnabled = false;
        }

        private void interviewButton_Click(object sender, RoutedEventArgs e)
        {
            var selected = (KeyValuePair<Student, ResidencyPlacement.Application>)(appliedDataGrid.SelectedItem);
            InterviewStudent interviewStudent = new InterviewStudent();
            interviewStudent.Owner = this;
            interviewStudent.Title = "Enter Interview Mark for " + selected.Key.Name;
            interviewStudent.ShowDialog();

            if (interviewStudent.DialogResult == true)
            {
                try
                {
                    App.ResSystem.StudentAttendsInterview(selected.Key, hospital, interviewStudent.InterviewMark);
                    interviewedDataGrid.Items.Refresh();
                }
                catch
                {

                }
            }
        }

        private void offerButton_Click(object sender, RoutedEventArgs e)
        {
            var selected = (KeyValuePair<Student, ResidencyPlacement.Application>)(interviewedDataGrid.SelectedItem);

            try
            {
                App.ResSystem.StudentIsSelectedAndOffered(selected.Key, hospital);
                selectedDataGrid.Items.Refresh();
            }
            catch
            {

            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string p)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(p));
            }
        }

        public Hospital Hospital
        {
            set
            {
                this.hospital = value;
                NotifyPropertyChanged("Hospital");
            }
        }
    }
}
